package pl.PawelAndAlicja.badugiproject.main.Gui;

import pl.PawelAndAlicja.badugiproject.main.Players.ClientPlayer;
import pl.PawelAndAlicja.badugiproject.main.Players.FactoryPlayer;
import pl.PawelAndAlicja.badugiproject.main.Players.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

class ClientGUI extends JFrame{

    /**
     * Glowny konstruktor ktory tworzy cala ramke, menu, przyciski i opcje.
     * W klasie zawarte sa listenery (sluchacze okien) ktore sa odpowiedzialne
     * za uruchomienie konkretnej operacji. Ponadto klasa usprawnia dzialanie okna do kazdego rodzaju rozdzielczosci ekranu.
     *
     */
    public ClientGUI() {
		/*
		 * Rozmiar programu 1) Sprawdzenie rozmiaru
		 */
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;

		/*
		 * Rozmiar programu 2) Okreslenie rozmiaru
		 */
        setSize(screenWidth, screenHeight );

		/*
		 * Pozwalamy systemowi na ustawienie okna w obrebie ekranu
		 */
        setLocationByPlatform(true);

		/*
		 * Ustawienie ikony i tytulu okna
		 */

        Image img = kit.getImage(pathImg + "icon_16" + File.separator
                + "Filter.png");
        setIconImage(img);
        setTitle("Badugi Poker by Jan Koszela & Alicja Gromotowicz");

        /*
         * Ustawienie tła okna
         */

        background = new JLabel(new ImageIcon(pathImg + "background.png"));
        add(background);
        background.setLayout(new FlowLayout());



		/*
		 * ********** PASEK MENU *********************
		 */

		/*
		 * Opcje Menu
		 */
        JMenu fileMenu = new JMenu("Plik");
        final JMenu actionMenu = new JMenu("Akcje");
        JMenu helpMenu = new JMenu("Pomoc");

		/*
		 * Dodanie elementow menu 1) File Menu
		 */
        final JMenuItem newGameImageItem = new JMenuItem("Nowa gra", new ImageIcon(
                pathImg + "icons" + File.separator + "icon_16" + File.separator + "New-game-icon_16.png"));
        final JMenuItem addBotImageItem = new JMenuItem("Dodaj Bot'a", new ImageIcon(
                pathImg + "icons" + File.separator + "icon_16" + File.separator + "End-game-icon_16.png"));
        final JMenuItem endBatchImageItem = new JMenuItem("Zakoncz partie", new ImageIcon(
                pathImg + "icons" + File.separator + "icon_16" + File.separator + "End-game-icon_16.png"));
        JMenuItem exitImageItem = new JMenuItem("Zakoncz", new ImageIcon(
                pathImg + "icons" + File.separator + "icon_16" + File.separator + "cross.png"));


        fileMenu.add(newGameImageItem);
        fileMenu.addSeparator();
        fileMenu.add(addBotImageItem);
        fileMenu.add(endBatchImageItem);
        fileMenu.addSeparator();
        fileMenu.add(exitImageItem);

		/*
		 * 2) Action Menu
		 */
        JMenuItem checkItem = new JMenuItem("Sprawdzam", new ImageIcon(pathImg
                + "icons" + File.separator + "icon_16" + File.separator + "Check-icon_16.png"));
        JMenuItem betItem = new JMenuItem("Stawiam", new ImageIcon(pathImg
                + "icons" + File.separator + "icon_16" + File.separator + "bet-icon_16.png"));
        JMenuItem raiseItem = new JMenuItem("Przebijam", new ImageIcon(pathImg
                + "icons" + File.separator + "icon_16" + File.separator + "Raise-icon_16.png"));
        final JMenuItem callItem = new JMenuItem("Wyrównuję", new ImageIcon(pathImg
                + "icons" + File.separator + "icon_16" + File.separator + "Call-icon_16.png"));
        JMenuItem foldItem = new JMenuItem("Pasuję", new ImageIcon(pathImg
                + "icons" + File.separator + "icon_16" + File.separator + "Fold-icon_16.png"));
        JMenuItem allInItem = new JMenuItem("Idę All-In", new ImageIcon(pathImg
                + "icons" + File.separator + "icon_16" + File.separator + "All-In-icon_16.png"));

        actionMenu.add(checkItem);
        actionMenu.add(betItem);
        actionMenu.add(raiseItem);
        actionMenu.add(callItem);
        actionMenu.add(foldItem);
        actionMenu.add(allInItem);



		/*
		 * 5) Help Menu
		 */
        JMenuItem aboutItem = new JMenuItem("O programie", new ImageIcon(
                pathImg + "icons" + File.separator + "icon_16" + File.separator + "info.png"));

        helpMenu.add(aboutItem);


        JMenuBar menuBar = new JMenuBar();

		/*
		 * Dodanie obiektow paska
		 */

        menuBar.add(fileMenu);
        menuBar.add(actionMenu);
        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

		/*
		 * *********** PASEK SZYBKICH OPCJI ******************
		 */
        final JButton checkQuickButton = new JButton(new ImageIcon(pathImg + "icons" + File.separator + "icon_24" + File.separator + "Check-icon_24.png"));
        final JButton betQuickButton = new JButton(new ImageIcon(pathImg + "icons" + File.separator + "icon_24" + File.separator + "bet-icon_24.png"));
        final JButton raiseQuickButton = new JButton(new ImageIcon(pathImg + "icons" + File.separator + "icon_24" + File.separator + "Raise-icon_24.png"));
        final JButton callQuickButton = new JButton(new ImageIcon(pathImg	+ "icons" + File.separator + "icon_24" + File.separator + "Call-icon_24.png"));
        final JButton foldQuickButton = new JButton(new ImageIcon(pathImg	+ "icons" + File.separator + "icon_24" + File.separator + "Fold-icon_24.png"));
        final JButton allInQuickButton = new JButton(new ImageIcon(pathImg + "icons" + File.separator + "icon_24" + File.separator + "All-In-icon_24.png"));

        final JToolBar quickToolsBar = new JToolBar("Szybkie opcje", SwingConstants.HORIZONTAL);
        quickToolsBar.add(checkQuickButton);
        quickToolsBar.add(betQuickButton);
        quickToolsBar.add(raiseQuickButton);
        quickToolsBar.addSeparator();
        quickToolsBar.add(callQuickButton);
        quickToolsBar.add(foldQuickButton);
        quickToolsBar.addSeparator();
        quickToolsBar.add(allInQuickButton);


        this.add(quickToolsBar, BorderLayout.SOUTH);

		/*
		 * Akcje przyciskow z paska
		 */
        checkQuickButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                checkAction();
            }
        });
        betQuickButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                betAction();
            }
        });
        raiseQuickButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                raiseAction();
            }
        });
        callQuickButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                callAction();
            }
        });
        foldQuickButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                foldAction();
            }
        });
        allInQuickButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                allInAction();
            }
        });

		/*
		 * Dymki ikon
		 */
        checkQuickButton.setToolTipText("Sprawdzam karty");
        betQuickButton.setToolTipText("Stawiam stawke...");
        raiseQuickButton.setToolTipText("Przebijam obecna stawke");
        callQuickButton.setToolTipText("Wyrównuje");
        foldQuickButton.setToolTipText("Pasuje");
        allInQuickButton.setToolTipText("Wchodzę All-In");



        /*
        *   ActionListenery
        */
        ////////////// MENU
        newGameImageItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                newGameImageItem.setEnabled(false);
                endBatchImageItem.setEnabled(true);

                actionMenu.setEnabled(true);

                checkQuickButton.setEnabled(true);
                betQuickButton.setEnabled(true);
                raiseQuickButton.setEnabled(true);
                callQuickButton.setEnabled(true);
                foldQuickButton.setEnabled(true);
                allInQuickButton.setEnabled(true);

                newGameActionOption();
            }
        });

        addBotImageItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                addBotPlayerActionOption();
            }
        });

        endBatchImageItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                newGameImageItem.setEnabled(true);
                endBatchImageItem.setEnabled(false);

                actionMenu.setEnabled(false);

                checkQuickButton.setEnabled(false);
                betQuickButton.setEnabled(false);
                raiseQuickButton.setEnabled(false);
                callQuickButton.setEnabled(false);
                foldQuickButton.setEnabled(false);
                allInQuickButton.setEnabled(false);

                endBatchActionOption();
            }
        });

        exitImageItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        /////////// AKCJE
        checkItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                checkAction();
            }
        });

        betItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                betAction();
            }
        });

        raiseItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                raiseAction();
            }
        });

        callItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                callAction();
            }
        });

        foldItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                foldAction();
            }
        });

        allInItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                allInAction();
            }
        });

        //////// O programie
        aboutItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                aboutActionOption();
            }
        });


        //////////////////////////////
        /*
        * Ustawienie aktywnych ikon
        */
        endBatchImageItem.setEnabled(false);
        actionMenu.setEnabled(false);

        checkQuickButton.setEnabled(false);
        betQuickButton.setEnabled(false);
        raiseQuickButton.setEnabled(false);
        callQuickButton.setEnabled(false);
        foldQuickButton.setEnabled(false);
        allInQuickButton.setEnabled(false);
    }

//    public void setSettingsClient(String gameName, String smallBlind, String bigBlind) {
//        this.titleTable = gameName;
//        this.smallBlindAmounts = smallBlind;
//        this.bigBlindAmounts = bigBlind;
//        this.setTitle(gameName);
//    }

    public Player getPlayer() {
        return player;
    }

    private void addBotPlayerActionOption() {
    }

    private void aboutActionOption() {
        AboutJDialog aboutJDialog = new AboutJDialog(this);
        aboutJDialog.setVisible(true);

    }

    private void allInAction() {

    }

    private void foldAction() {

    }

    private void callAction() {

    }

    private void raiseAction() {

    }

    private void betAction() {

    }

    private void checkAction() {

    }

    private void endBatchActionOption() {

        table.removeAllTable();

        table.remove(table.getTablePanel());
        table.add(new JPanel());
        table.invalidate();
        table.validate();



        background.remove(table);
        background.add(new JPanel());
        background.invalidate();
        background.validate();


        this.getContentPane().remove(background);
        this.add(new JLabel());
        this.getContentPane().invalidate();
        this.getContentPane().validate();




//        background= new JLabel(new ImageIcon(pathImg + "background.png"));
        background.setIcon(new ImageIcon(pathImg + "background.png"));
        background.setVisible(true);
        this.add(background);

        this.setTitle("Badugi Poker by Jan Koszela & Alicja Gromotowicz");

//        background.remove(table);
    }


    private void newGameActionOption() {

        FactoryPlayer factoryPlayer = new FactoryPlayer();
        ClientPlayer clientPlayer = new ClientPlayer(factoryPlayer);

        player = clientPlayer.createPlayer("Human");
//        clientPlayer.listenSocket(345);
        player.setName(JOptionPane.showInputDialog("Podaj nick:"));
        player.setPrizeTokens(6000);

        System.out.println(player);

        NewGameJDialog newGameJDialogWindow = new NewGameJDialog(this);

        newGameJDialogWindow.setVisible(true);


        background.setIcon(new ImageIcon(pathImg + "pokerTable.png"));
        table = new Table(this);
        table.setVisible(true);
        background.add(table.getTablePanel());

    }

    /*
     * Dostep do katalogu "img"
     */
    private static final String pathImg = "src" + File.separator
            + "pl" + File.separator + "PawelAndAlicja" + File.separator +
            "badugiproject" + File.separator + "main" + File.separator +
            "Gui" + File.separator + "images" + File.separator;

    private JLabel background;

//    private String bigBlindAmounts;
//    private String smallBlindAmounts;
//    private String titleTable;

    private Table table;                            // Panel z wygladem i przebiegiem rozgrywki

    private Player player;

}
